/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package applet.source.action;

import algo.graph.DijkstraAlgorithm;
import applet.visualisation.VisuApp;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import prefuse.controls.ControlAdapter;
import prefuse.visual.EdgeItem;
import prefuse.visual.NodeItem;
import prefuse.visual.VisualItem;

/**
 *
 * @author krishen
 */
public class ImpDijkstraShortPath extends ControlAdapter{

    private NodeItem nodeStart = null;
    private NodeItem nodeEnd = null;

    public void itemClicked(VisualItem item, MouseEvent e) {


         if ( item instanceof NodeItem ){

                 

                if (VisuApp.djistra_choice_1 == -1){
                    VisuApp.djistra_choice_1 = item.getRow();
                    nodeStart = (NodeItem)item;

                    Iterator tuples = nodeStart.getGraph().tuples();
                    Object obj = null;
                    NodeItem n = null;
                    while (tuples.hasNext() )
                    {

                        obj = tuples.next();
                        if (obj instanceof prefuse.data.Node) {
                            n = (NodeItem) obj;
                            // on remet a zero tout les noeuds
                            n.setSize(1);

                        }
                    }
                }else if (nodeStart != (NodeItem)item){
                    VisuApp.djistra_choice_2 = item.getRow();
                    nodeEnd = (NodeItem)item;
                    // on lance le calcul entre les deux noeuds
                    StartDijkstra();

                }

                if (VisuApp.djistra_choice_2 != -1){

                    // RAZ des choix
                    VisuApp.djistra_choice_1 = -1;
                    VisuApp.djistra_choice_2 = -1;
                }

        }
 
    }

    /**
     * On lance le processus de calcul de Dijkstra
     */

    private void StartDijkstra (){

       if (nodeStart != null && nodeEnd !=null){

           System.out.println("Dijkstraa ");
            DijkstraAlgorithm da = new DijkstraAlgorithm(nodeStart);
            da.execute();


            LinkedList<NodeItem> path = da.getPath(nodeEnd);
            for (NodeItem noeud : path) {
                noeud.setSize(2);
           	//System.out.println("****** Node = "+noeud.getString("name"));
	   }
       }

    }

}